﻿using Newtonsoft.Json.Linq;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIAddFoodsItem : UIUserControl
    {
        public UIAddFoodsItem()
        {
            InitializeComponent();
        }

        public event EventHandler OnValueChanged;
        public event EventHandler OnAddValueError;

        public string Type { get; set; }

        public string Food
        {
            get => lblName.Text;
            set => lblName.Text = value;
        }

        public string Money
        {
            get => lblPrice.Text;
            set => lblPrice.Text = value;
        }

        public string Tag
        {
            get => lblValue.Tag.ToString();
            set
            {
                lblValue.Tag = value.ToString();
            }
        }

        public int Count
        {
            get => lblValue.Text.ToInt();
            set
            {
                lblValue.Text = value.ToString();
                lblValue.Visible = btnCut.Visible = value > 0;
            }
        }

        /// <summary>
        /// 最大数量
        /// </summary>
        public int MaxCount
        {
            get => btnAdd.Tag.ObjToInt(0);
            set
            {
                btnAdd.Tag = value.ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int maxCount = btnAdd.Tag.ObjToInt(0);
            if ((maxCount == 0) || (Count < maxCount) && Count < 99999999)
            {
                Count++;
                OnValueChanged?.Invoke(this, EventArgs.Empty);
            }
            else {
                OnAddValueError?.Invoke(this, EventArgs.Empty);
            }
            Invalidate();
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            if (Count > 0)
            {
                Count--;
                OnValueChanged?.Invoke(this, EventArgs.Empty);
                Invalidate();
            }
        }

        private void UIAddFoodsItem_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            
            if (Count > 0)
            {
                (sender as UIUserControl).FillColor = Color.PapayaWhip;
                (sender as UIUserControl).RectColor = Color.Orange;
                pnlCommand.FillColor = Color.PapayaWhip;
                pnlCommand.FillColor2 = Color.PapayaWhip;
                pnlCommand.RectColor = Color.Orange;
                btnCut.RectColor = Color.Orange;
                btnAdd.RectColor = Color.Orange;
            }
            else
            {
                (sender as UIUserControl).FillColor = Color.White;
                (sender as UIUserControl).RectColor = Color.Silver;
                pnlCommand.FillColor = Color.White;
                pnlCommand.FillColor2 = Color.White;
                pnlCommand.RectColor = Color.Silver;
                btnCut.RectColor = Color.Silver;
                btnAdd.RectColor = Color.Silver;
            }
        }
    }
}
